package com.supermap.application.service;

import com.supermap.application.common.Region;
import com.supermap.application.common.RegionSupport;
import com.supermap.application.dao.RegionInfoDao;
import com.supermap.application.entity.TRegioninfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RegionInfoService
 * @Description TODO
 * @Author HEKE
 * @Date 2019/2/26 15:07
 */
@Service
public class RegionInfoService {

    @Autowired
    private RegionInfoDao regionInfoDao;

    /**
     * 获取选中区划下指定级别下的所有行政区划
     * @param catalogid
     * @param regions
     * @param level   获取的级别（要考虑该级别可能小于regions里的级别）
     * @return
     */
    public List<TRegioninfo> findSelRegions2(int catalogid, Region[] regions, int level) {
        if (regions == null) {
            return null;
        }
        List<TRegioninfo> regioninfoList = new ArrayList<TRegioninfo>();
        for (int i = 0, len = regions.length; i < len; i++) {
            Region r = regions[i];
            if (r.getRegionCode().contains("#")) {
                boolean isMunicipal = false;
                int regionlevel = r.getRegionLevel();
                String regioncode = r.getRegionCode();
                isMunicipal = RegionSupport.isMunicipalRegion(regioncode);
                if (regioncode.length() == 12) {
                    regioncode = subStrCode(regioncode);
                    List re = regionInfoDao.findSelRegions(catalogid, regionlevel, regioncode + "%");
                    regioninfoList.addAll(re);
                } else {// 特色区域处理
                    regioncode = r.getRegionCode().replaceAll("#", "0");
                    regioncode = subStrCode(regioncode);
                    // 所选树节点上一级节点
                    int maxLevel = RegionSupport.getRegionLevel(regioncode);
                    int qlevel = -1;
                    if (isMunicipal && maxLevel >= 2) {
                        qlevel = maxLevel + 2;
                    } else {
                        qlevel = maxLevel + 1;
                    }
                    // 处理省级和特色经济区
                    regioncode = regioncode.substring(2);
                    List re = regionInfoDao.findSelRegions(catalogid, qlevel, "%" + regioncode);
                    regioninfoList.addAll(re);
                }
            } else {
                int regionlevel = r.getRegionLevel();
                String regioncode = r.getRegionCode();
//              int maxLevel = RegionSupport.getRegionLevel(r.getRegionCode());
                if (level == regionlevel) {
                    String[] codes = new String[]{regioncode};
                    List re = regionInfoDao.findByReginCodes(catalogid, codes);
                    regioninfoList.addAll(re);
                } else if (regionlevel < level) {
                    regioncode = RegionSupport.trimRight(regioncode, '0');
                    List<TRegioninfo> infoList = regionInfoDao.findSelRegions(catalogid, level, regioncode + "%");
                    regioninfoList.addAll(infoList);
                } else if (regionlevel > level) {
                    regioncode = RegionSupport.getSubstr(regioncode, level);
                    switch (level) {
                        case 2:
                            regioncode += "0000000000";
                            break;
                        case 3:
                            regioncode += "00000000";
                            break;
                        case 4:
                            regioncode += "000000";
                            break;
                        case 5:
                            regioncode += "000";
                            break;
                    }

                    String[] codes = new String[]{regioncode};
                    List re = regionInfoDao.findByReginCodes(catalogid, codes);
                    regioninfoList.addAll(re);
                }
            }


        }

        return regioninfoList;
    }


    /**
     * 统一用来截取 所有#之前的字符
     *
     * @param regioncode
     *            行政区划编码
     * @return 截取后的编码
     */
    private String subStrCode(String regioncode) {
        String subStr = regioncode;
        if (subStr.contains("#")) {// 下级所有
            subStr = subStr.substring(0, subStr.indexOf("#"));
            if (RegionSupport.isMunicipalRegion(subStr)
                    && subStr.indexOf("00") != -1) {
                subStr = subStr.substring(0, subStr.indexOf("00"));// 取直辖市的前两位
            }
        } else if (subStr.equals("00000000000")) {
            subStr = "";
        }
        return subStr;
    }
}
